package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.GoodBean;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.mapper.GoodMapper;
import com.lovo.stock.service.IGoodService;
import com.lovo.stock.service.IPurcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, GoodBean> implements IGoodService {
    @Autowired
    GoodMapper goodMapper;


    @Override
    public List<GoodBean> queryGoodBean() {
        return goodMapper.selectList(null);
    }

    @Override
    public IPage<GoodBean> queryGoodByPage(PageInfo<GoodBean> pageInfo) {
        QueryWrapper q=new QueryWrapper<>();
        //动态判断,首先判断商品是否为空
        if (pageInfo.getData()!=null){
            //商品不为空
            if (pageInfo.getData().getGoodName()!=null&&!"".equals(pageInfo.getData().getGoodName())){
                q.like("good_name",pageInfo.getData().getGoodName());
            }
            //类别选择
            if (pageInfo.getData().getGoodCategory()!=null&&!"".equals(pageInfo.getData().getGoodCategory())){
                q.eq("good_category",pageInfo.getData().getGoodCategory());
            }
        }
        if (pageInfo.getStatus()!=null){
            //已定销售价(1不限，0未定)
            if (pageInfo.getStatus()==0){
                q.eq("good_sale_price",0);
            }
        }

        //起始数量
        if (pageInfo.getStartNum()!=null && pageInfo.getStartNum()>0){
            q.ge("good_inventory",pageInfo.getStartNum());
        }
        //结束数量
        if (pageInfo.getEndNum()!=null && pageInfo.getEndNum()>0){
            q.le("good_inventory",pageInfo.getEndNum());
        }
        IPage<GoodBean> page=goodMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()),q);
        return page;
    }

    @Override
    public GoodBean queryGoodById(int id) {
        return goodMapper.selectById(id);
    }

    @Override
    public GoodBean queryGoodByGoodNum(String goodNum) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("good_num",goodNum);
        return goodMapper.selectOne(q);
    }

    @Override
    public void addGood(GoodBean goodBean) {
        //判断有无该商品
        GoodBean good=this.queryGoodByGoodNum(goodBean.getGoodNum());
        //如果没有该商品
        if (good==null){
            goodMapper.insert(goodBean);
        }
    }

    @Override
    public void addGoods(List<GoodBean> goodBeans) {
        for (int i = 0; i < goodBeans.size(); i++) {
            //更新商品库存与采购价
            goodMapper.addGoods(goodBeans.get(i));
        }
    }

    @Override
    public void reduceGoods(List<GoodBean> goodBeans) {
        for (int i = 0; i < goodBeans.size(); i++) {
            //更新商品库存与采购价
            goodMapper.reduceGoods(goodBeans.get(i));
        }
    }

/*    @Override
    public void addGoods(List<GoodBean> goodBeans) {
        goodMapper.insertGoods(goodBeans);
    }*/

    @Override
    public void updateGood(GoodBean goodBean) {
        goodMapper.updateById(goodBean);
    }
}
